# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/20 15:58
desc: 登录页面
'''
import time

from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions as EC
import logging
from selenium.webdriver.support.wait import WebDriverWait
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage


class LoginPage(CLUEAppBasePage):
    _reg_or_login_android = (AppiumBy.XPATH,"//*[contains(@text,'注册/登录')]")

    _switch_login_android = (AppiumBy.XPATH, "//*[@resource-id='login.ldap']")
    _switch_login_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeImage"')

    _std_login_android = (AppiumBy.XPATH, "//*[contains(@text,'Standard')]")
    _std_login_ios = (AppiumBy.ACCESSIBILITY_ID, "Standard")

    _uname_locator_android = (AppiumBy.XPATH, "//*[contains(@text,'请输入用户名')]/../android.widget.EditText")
    _uname_locator_ios = (AppiumBy.IOS_PREDICATE,'type == "XCUIElementTypeTextField"')

    _pwd_locator_android = (AppiumBy.XPATH, "//*[contains(@text,'请输入密码')]/../android.widget.EditText")
    _pwd_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSecureTextField"')

    #_captcha_locator = (AppiumBy.XPATH, "//*[contains(@text,'请输入验证码')]/../android.widget.EditText")
    #_agree_btn_android = (AppiumBy.XPATH, "//*[@resource-id='login.password']/../following-sibling::*[1]/child::*[1]")
    _agree_check_android = (AppiumBy.XPATH, "//*[@resource-id='login.checkbox']/preceding-sibling::*[1]")
    _agree_check_ios = (AppiumBy.XPATH, '//XCUIElementTypeStaticText[@name="我已阅读并同意"]/../preceding-sibling::*[1]')

    #_login_btn_android = (AppiumBy.XPATH, "//*[contains(@text,'登录')]")
    _login_btn_android = (AppiumBy.XPATH, "//*[@resource-id='login.checkbox']/../following-sibling::*[1]")
    _login_btn_ios = (AppiumBy.ACCESSIBILITY_ID, "登录")

    def click_agree_checkbox(self):
        """点击Standard登录方式的“我已阅读并同意”各项协议checkbox(IOS云真机下每次XPATH变更)

        :return:
        """
        try:
            self.find_element_and_click(getattr(self, '_agree_check_' + self.os))
        except Exception as e:
            if self.os=='ios':
                self._agree_check_ios = (AppiumBy.XPATH, '//XCUIElementTypeStaticText[@name="我已阅读并同意"]/preceding-sibling::*[1]')
                self.find_element_and_click(getattr(self, '_agree_check_' + self.os))
            else:
                raise e



    def login(self, username, password, captcha='123456'):
        from proj_spec.CLUE.mobile_po.main_page import MainPage # 解决循环导入
        #if "欢迎来到Clue" in self.driver.page_source:
        try:
            self.find_element_and_click(getattr(self, '_reg_or_login_' + self.os),timeout=1)
        except Exception: #页面正常跳转到登录页
            pass

        try:
            #time.sleep(2) # 页面加载
            self.find_element_and_click(getattr(self, '_switch_login_' + self.os))
            self.find_element_and_click(getattr(self, '_std_login_' + self.os))
            self.find_element_and_input(getattr(self, '_uname_locator_' + self.os), username)
            self.find_element_and_input(getattr(self, '_pwd_locator_' + self.os), password)
            #self.find_element_and_click(getattr(self, '_agree_check_' + self.os))
            self.click_agree_checkbox()
            # login_btn = self.find_elements(getattr(self, '_login_btn_' + self.os))[2]
            # login_btn.click()
            self.find_element_and_click(getattr(self, '_login_btn_' + self.os))
        except Exception as e:
            logging.error("locating login elements failed")
            raise e





